package com.example.mypaging;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer;
import androidx.paging.DataSource;
import androidx.paging.LivePagedListBuilder;
import androidx.paging.PagedList;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView ry;
    private Button insert, clear;
    private StudentDao dao;
    private StudentsDatabase instance;
    private MyPagedAdapter adapter;
    private LiveData AllStudent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ry = findViewById(R.id.lis);
        insert = findViewById(R.id.insert);
        clear = findViewById(R.id.clear);
        adapter = new MyPagedAdapter();
        ry.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        ry.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        ry.setAdapter(adapter);
        instance = StudentsDatabase.getInstance(this);
        dao = instance.getStudentDao();
        AllStudent = new LivePagedListBuilder<>( instance.getStudentDao().getAllStudent(), 5).build();
        AllStudent.observe(this, new Observer<PagedList<Student>>() {
            @Override
            public void onChanged(PagedList<Student> students) {
                adapter.submitList(students);
            }
        });
        insert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Student[] students = new Student[100];
                for (int i = 0; i < 100; i++) {
                    Student student = new Student();
                    student.setStudentnunber(i);
                    students[i] = student;
                }
                new InsertAsynTask(dao).execute(students);
            }
        });
        clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new DeleteAsynTask(dao).execute();
            }
        });
    }

    static class InsertAsynTask extends AsyncTask<Student, Void, Void> {
        StudentDao studentDao;

        public InsertAsynTask(StudentDao studentDao) {
            this.studentDao = studentDao;
        }

        @Override
        protected Void doInBackground(Student... students) {
            studentDao.insertStudent(students);
            return null;
        }
    }

    static class DeleteAsynTask extends AsyncTask<Void, Void, Void> {
        StudentDao studentDao;

        public DeleteAsynTask(StudentDao studentDao) {
            this.studentDao = studentDao;
        }

        @Override
        protected Void doInBackground(Void... voids) {
            studentDao.deleteAllStudent();
            return null;
        }
    }
}